package com.example.util;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.example.common.ConstantConfig;

public class AliSendMessageUtil {
	
    //产品名称:云通信短信API产品,开发者无需替换
    static final String product = "Dysmsapi";
    //产品域名,开发者无需替换
    static final String domain = "dysmsapi.aliyuncs.com";
    //短信签名
    static final String signName = "福能达";
    
    /**
     * 阿里云短信发送
     * 
     * @param phone 手机号码
     * @param templateCode 短信模板-可在短信控制台中找到
     * @param templateParam 模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时, request.setTemplateParam("{\"name\":\"Tom\", \"code\":\"123\"}");
     * @throws ClientException 
     * @throws ServerException 
     */
	public static  void sendMessage(String phone , String templateCode ,JSONObject templateParam) throws ServerException, ClientException {
        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", ConstantConfig.getConfig("aliyuncs.accessKey"), ConstantConfig.getConfig("aliyuncs.secretKey"));
		DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);
        SendSmsRequest request = new SendSmsRequest();
        request.setPhoneNumbers(phone);
        request.setSignName(signName);
        request.setTemplateCode(templateCode);;
        request.setTemplateParam(templateParam.toString());
        acsClient.getAcsResponse(request);
    }
}
